# coding=utf8

from collections import defaultdict
import re
import random
import copy


def sub_space_to_underline(type_):
    return type_.replace(' ', '_')

lines = open('../data/article_categories_en.ttl').readlines()
f_write = open('../data/intermediate/negative_example.txt', 'w')
regex = r'<http://dbpedia.org/resource/(.+?)> <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:(.+?)>'

types_to_instace_set = defaultdict(set)
for line in lines:
    m = re.match(regex, line)
    if m:
        types_to_instace_set[m.group(2)].add(m.group(1))
    else:
        print 'no match', line

lines = open('../data/categorySizeSorted.txt').readlines()
type_list = [line.split('\t:\t')[0] for line in lines]
type_list_copy = copy.copy(type_list)
for type1 in type_list: 
    f_write.write(type1)
    set1 = types_to_instace_set[sub_space_to_underline(type1)]
    type_cnt = 1
    random.shuffle(type_list_copy)
    for type2 in type_list_copy:
        set2 = types_to_instace_set[sub_space_to_underline(type2)]
        if not set1.intersection(set2):
            type_cnt += 1
            f_write.write('\t:\t'+type2)
        if type_cnt>=100:
            break
    f_write.write('\n')

f_write.close()

